package com.yida.gwj.config.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yida.common.core.domain.R;
import com.yida.common.core.enums.UserStatus;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.domain.BaseEntity;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.InnerAuth;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.api.domain.GwjConfStep;
import com.yida.gwj.common.utils.GwjConvertUtil;
import com.yida.gwj.config.domain.dto.GwjConfStepConfigDTO;
import com.yida.gwj.config.domain.dto.GwjConfStepDTO;
import com.yida.gwj.config.domain.request.GwjConfStepConfigRequest;
import com.yida.gwj.config.domain.request.GwjConfStepRequest;
import com.yida.gwj.config.service.IGwjConfStepService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 业务步骤Controller
 * <p>
 * 修订20230112 hmh(修改地址，方法名替换，重复对象构建，BUG修复，重复循环)
 *
 * @author lqq
 * @date 2022-12-12
 */
@RestController
@RequestMapping("/step")
@Api(tags = "步骤信息 API")
public class GwjConfStepController extends BaseController {

    @Autowired
    private IGwjConfStepService gcStepService;

    /**
     * 查询业务步骤列表
     */
    @GetMapping("/list")
    @RequiresPermissions("gwj:config:step:list")
    public TableDataInfo list(GwjConfStep gcStep) {
        // 只查未删除的
        gcStep.setDelFlag(UserStatus.OK.getCode());
        startPage();
        List<GwjConfStep> list = gcStepService.selectGwjConfStepList(gcStep);
        return GwjConvertUtil.convertTable(getDataTable(list), GwjConfStepDTO.class);
    }

    /**
     * 查询业务步骤列表
     */
    @GetMapping("/list/all")
    @RequiresPermissions("gwj:config:step:list")
    public AjaxResult listAll() {
        List<GwjConfStep> list = gcStepService.selectGwjConfStepList(new GwjConfStep());
        if (StringUtils.isEmpty(list)) {
            return AjaxResult.success(new ArrayList<>());
        }
        return AjaxResult.success(GwjConvertUtil.convertList(list, GwjConfStepDTO.class));
    }

    /**
     * 查询业务步骤列表
     */
    @GetMapping("/list/allValid")
    @RequiresPermissions("gwj:config:step:list")
    public AjaxResult listAllValid() {
        List<GwjConfStep> list = gcStepService.selectGwjConfStepListByStatus(UserStatus.OK.getCode());
        if (StringUtils.isEmpty(list)) {
            return AjaxResult.success(new ArrayList<>());
        }
        return AjaxResult.success(GwjConvertUtil.convertList(list, GwjConfStepDTO.class));
    }

    /**
     * 导出业务步骤列表
     */
    @PostMapping("/export")
    @RequiresPermissions("gwj:config:step:export")
    @Log(title = "业务步骤", businessType = BusinessType.EXPORT, systemName = "gwj")
    public void export(HttpServletResponse response, GwjConfStep gcStep) {
        List<GwjConfStep> list = gcStepService.selectGwjConfStepList(gcStep);
        ExcelUtil<GwjConfStep> util = new ExcelUtil<>(GwjConfStep.class);
        util.exportExcel(response, list, "业务步骤数据");
    }

    /**
     * 获取业务步骤详细信息
     */
    @GetMapping(value = "/{stepId}")
    @RequiresPermissions("gwj:config:step:query")
    public AjaxResult getInfo(@PathVariable("stepId") Long stepId) {
        return AjaxResult.success(GwjConvertUtil.convert(gcStepService.selectByGwjConfStepId(stepId), GwjConfStepDTO.class));
    }

    /// **
    // * 新增业务步骤
    // */
    // @PostMapping
    // @RequiresPermissions("gwj:config:step:add")
    // @Log(title = "业务步骤", businessType = BusinessType.INSERT, systemName = "gwj")
    // public AjaxResult add(@Validated(BaseEntity.Save.class) @RequestBody GwjConfStep gcStep) {
    // gcStepService.insertGwjConfStep(gcStep);
    // return AjaxResult.success();
    // }

    /**
     * 新增业务步骤
     * <p>
     * 修订:20230224
     */
    @PostMapping
    @RequiresPermissions("gwj:config:step:add")
    @Log(title = "业务步骤", businessType = BusinessType.INSERT, systemName = "gwj")
    public AjaxResult add(@Validated(BaseEntity.Save.class) @RequestBody GwjConfStepRequest gwjConfSetpRequest) {
        String stepName = gwjConfSetpRequest.getStepName();
        String remark = gwjConfSetpRequest.getRemark();
        if (StringUtils.isNull(stepName)) {
            throw new ServiceException("步骤名称不能为空！");
        }
        if (StringUtils.isNull(remark)) {
            throw new ServiceException("备注信息不能为空！");
        }
        gcStepService.insertGwjConfStep(gwjConfSetpRequest);
        return AjaxResult.success();
    }

    // /**
    // * 修改业务步骤
    // */
    // @PutMapping
    // @RequiresPermissions("gwj:config:step:edit")
    // @Log(title = "业务步骤", businessType = BusinessType.UPDATE, systemName = "gwj")
    // public AjaxResult edit(@Validated(BaseEntity.Update.class) @RequestBody GwjConfStep gcStep) {
    // gcStepService.updateGwjConfStep(gcStep);
    // return AjaxResult.success();
    // }

    /**
     * 修改业务步骤
     * <p>
     * 修订:20230227
     *
     * @param gwjConfSetpRequest 公维金配置请求
     * @return
     */
    @PutMapping
    @RequiresPermissions("gwj:config:step:edit")
    @Log(title = "业务步骤", businessType = BusinessType.UPDATE, systemName = "gwj")
    public AjaxResult edit(@Validated(BaseEntity.Update.class) @RequestBody GwjConfStepRequest gwjConfSetpRequest) {
        gcStepService.updateGwjConfStep(gwjConfSetpRequest);
        return AjaxResult.success();
    }

    /**
     * 删除业务步骤
     */
    @DeleteMapping("/{stepIds}")
    @RequiresPermissions("gwj:config:step:remove")
    @Log(title = "业务步骤", businessType = BusinessType.DELETE, systemName = "gwj")
    public AjaxResult remove(@PathVariable Long[] stepIds) {
        return toAjax(gcStepService.deleteByGwjConfStepIds(stepIds));
    }

    /**
     * 根据业务标识查询步骤ID列表
     */
    @InnerAuth
    @GetMapping("/inner/stepIdList/{flagId}")
    public R<List<GwjConfStep>> selectGwjConfStepListByFlagId(@PathVariable("flagId") Integer flagId) {
        return R.ok(gcStepService.selectGwjConfStepListByFlagId(flagId));
    }

    /**
     * 查询下一个步骤
     */
    @InnerAuth
    @GetMapping("/inner/nextStep/{flagId}/{currentStep}")
    public R<GwjConfStep> selectNextGwjConfStepByFlagId(@PathVariable("flagId") Integer flagId, @PathVariable("currentStep") Long currentStep) {
        return R.ok(gcStepService.selectNextGwjConfStepByFlagId(flagId, currentStep));
    }

    /**
     * 查询上一个步骤
     */
    @InnerAuth
    @GetMapping("/inner/previousStep/{flagId}/{currentStep}")
    public R<GwjConfStep> selectPreviousGwjConfStepByFlagId(@PathVariable("flagId") Integer flagId, @PathVariable("currentStep") Long currentStep) {
        return R.ok(gcStepService.selectPreviousGwjConfStepByFlagId(flagId, currentStep));
    }

    // ----------------------------------新增功能区

    /**
     * 获取业务步骤详细信息
     */
    @ApiOperation(value = "获取业务步骤详细信息")
    @GetMapping(value = "/info/{stepId}")
    @RequiresPermissions("gwj:config:step:query")
    public AjaxResult getGwjConfStepInfo(@ApiParam(value = "步骤ID") @PathVariable("stepId") Long stepId) {
        return AjaxResult.success(gcStepService.queryGwjConfStepByGwjConfStepId(stepId));
    }

    /**
     * 获取业务步骤配置列表
     */
    @ApiOperation(value = "获取业务步骤配置列表", response = GwjConfStepConfigDTO.class, responseContainer = "List")
    @GetMapping(value = "/configList/{stepId}")
    @RequiresPermissions("gwj:config:step:query")
    public AjaxResult getGwjConfStepConfigList(@ApiParam(value = "步骤ID") @PathVariable("stepId") Long stepId) {
        return AjaxResult.success(gcStepService.selectGwjConfStepConfigListByGwjConfStepId(stepId));
    }

    /**
     * 新增/修改步骤配置信息
     *
     * @param gwjConfStepConfigRequest
     * @return
     */
    @ApiOperation(value = "新增/修改步骤配置信息")
    @PostMapping(value = "/stepConfig")
    @RequiresPermissions("gwj:config:step:query")
    public AjaxResult batchInsertGwjConfStepConfigRequest(@RequestBody GwjConfStepConfigRequest gwjConfStepConfigRequest) {
        gcStepService.batchInsertGwjConfStepConfigRequest(gwjConfStepConfigRequest);
        return AjaxResult.success("步骤配置成功");
    }

    // ----------------------------------新增功能区

    /**
     * 获取第一个步骤
     *
     * @param flagId 标识id
     * @return {@link AjaxResult}
     */
    @InnerAuth
    @ApiOperation(value = "获取第一个步骤", response = GwjConfStep.class)
    @GetMapping(value = "/inner/first/{flagId}")
    public R<GwjConfStep> getFirstStep(@PathVariable("flagId") Integer flagId) {
        return R.ok(gcStepService.getFirstStep(flagId));
    }

    /**
     * 获取最后一个步骤
     *
     * @param flagId 标识id
     * @return {@link AjaxResult}
     */
    @InnerAuth
    @GetMapping(value = "/inner/final/{flagId}")
    public R<GwjConfStep> getFinalStep(@PathVariable("flagId") Integer flagId) {
        return R.ok(gcStepService.getFinalStep(flagId));
    }

    /**
     * 获取全部步骤信息
     *
     * @param confStep 配置步骤
     * @return {@link R}<{@link List}<{@link GwjConfStep}>>
     */
    @InnerAuth
    @PostMapping(value = "/inner/selectConfStepList")
    public R<List<GwjConfStep>> selectConfStepList(@RequestBody GwjConfStep confStep) {
        return R.ok(gcStepService.selectGwjConfStepList(confStep));
    }

    /**
     * 获取业务步骤详细信息
     */
    @InnerAuth
    @GetMapping(value = "/selectByConfStepId")
    public R<GwjConfStep> selectByConfStepId(@RequestParam("stepId") Long stepId) {
        return R.ok(gcStepService.selectByConfStepId(stepId));
    }

    /**
     * 检测当前是否最后一步
     * 
     * 20230712
     * 
     * hmh
     * 
     */
    @InnerAuth
    @GetMapping(value = "/checkNextStepFinish")
    public R<JSONObject> checkNextStepFinish(@RequestParam("flagId") Integer flagId, @RequestParam("stepId") Long stepId) {
        return R.ok(gcStepService.checkNextStepFinish(flagId, stepId));
    }

    /**
     * 获取业务步骤配置列表
     */
    @InnerAuth
    @GetMapping(value = "/configList/inner")
    public R<JSONArray> getGwjConfStepConfigListInner(@RequestParam("stepId") Long stepId) {
        List<GwjConfStepConfigDTO> list = gcStepService.selectGwjConfStepConfigListByGwjConfStepId(stepId);
        if (list == null || list.size() == 0) {
            return R.fail("未获取到对应步骤配置信息！");
        }
        return R.ok(JSONArray.from(list));
    }

}
